<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <title>瞬感</title>
    <meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <style>
        * {
            margin: 0;
            padding: 0;
            user-select: none;
        }

        body {
            background-color: #eee;
        }

        div {
            color: black;
            font-size: 10px;
            width: 70%;
            margin-left: 15%;
            margin-top: 30px;
        }

        .tip {
            font-size: 22px;
            text-align: center;
            margin-top: 30%;
            font-weight: 700;
        }
		
		.grades{
			font-size: 12px;
			text-align: center;
			margin-top: 20px;
			display: block;
		}

		.list {
			text-align: center; /* 让文本内容水平居中 */
			display: flex;
			flex-direction: column;
			align-items: center; /* 让列表和文本一起水平居中 */
		}
	
		#rankinglist {
			list-style-position: inside; /* 将列表表头数字显示在列表项内部 */
		}

        #btn {
            border-radius: 8px;
            width: 100px;
            height: 32px;
            margin-top: 50px;
            margin-left: calc((100% - 100px) / 2);
        }
    </style>
</head>
<body>
    <p class="tip">反应速度测试</p>
    <div>点击开始后等待页面背景变色，变色后在最快时间点击页面任意位置</div>
	<p class="grades"></p>
    <button id="btn">开始测试</button>
	<div class="list">
		本地排行榜：
		<ol id="rankinglist"></ol>
    </div>
	<script>
        "use strict";

        function random(min, max) {
            let v = Math.floor(Math.random() * (max - min + 1)) + min;
            return v;
        }
		function listshow() {
		    let h_grades = localStorage.getItem("grades");
		    if (h_grades) {
		        h_grades = JSON.parse(h_grades);
		        let list = document.getElementById("rankinglist");
		        list.innerHTML = ""; // 清空列表内容
		        for (let i = 0; i < h_grades.length; i++) {
		            list.innerHTML += "<li>" + h_grades[i] + "</li>";
		        }
		    } else {
		        h_grades = [];
		        console.log("没有找到相关历史成绩");
		    }
		}

        let time = 0;
        let e;
		let allowClick;
        let btn = document.getElementById("btn");
		let grades = document.querySelector(".grades");
        function game() {
            btn.onclick = () => {
                btn.style.visibility = "hidden";
                allowClick = false;
				setTimeout(()=>{document.addEventListener('click', clickHandler);	},500)
				
                setTimeout(() => {
                    document.body.style.backgroundColor = "blue";
                    var start = Date.now();
                    e = setInterval(() => {
                        time = Date.now() - start;
                    }, 1);
                    allowClick = true; // 允许点击
                },random(1000, 5000));
            };
            
            function clickHandler() {
				console.log("hhh",e,allowClick);
                if (!e || !allowClick) {
                    // 提前点击，弹窗提醒
                    alert("禁止提前点击");
                    // 刷新页面
                    window.location.reload();
                } else if (e) { // 只有在允许点击的情况下才执行
                    clearInterval(e);
                    document.body.style.backgroundColor = "#eee";
                    btn.style.visibility = "visible";
                    grades.innerHTML = "你的反应速度为：" + time;
                    ranking(time); // 将新成绩添加到排行榜
                    time = 0;
                    e = 0;
                    document.removeEventListener('click', clickHandler); // 移除点击事件监听器
                }
            }
        }




		function ranking(newGrade) {
		    let h_grades = localStorage.getItem("grades");
		
		    if (h_grades) {
		        h_grades = JSON.parse(h_grades);
		    } else {
		        h_grades = [];
		    }
		
		    h_grades.push(newGrade);
		    h_grades.sort((a, b) => a - b);
		    const topfive = h_grades.slice(0, 5);
		    localStorage.setItem("grades", JSON.stringify(topfive));
		
		    // 更新排行榜
		    listshow();
		}

		
		
        function updateContainerWidth() {
            const container = document.querySelector('body');
            const windowWidth = window.innerWidth;
            const maxWidth = 300;
            const newWidth = Math.min(windowWidth, maxWidth);
            const marginLeft = (windowWidth - newWidth) / 2;

            container.style.width = newWidth + 'px';
            container.style.marginLeft = marginLeft + 'px';
        }

        updateContainerWidth();
        window.addEventListener('resize', updateContainerWidth);
		listshow();
        game();
    </script>
</body>
</html>
